package com.promise.jokerdream.work.faction

import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.response.pc.QueryFactionArmyResponse
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult
import com.promise.jokerdream.repository.foldWithApiException
import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.utils.AppContext

// 帮派远征工作
class FactionArmyWork(override val uin: String): BaseWork(uin) {
    
    override val name: String
        get() = "帮派远征"
    
    private val gameApi by lazy { GameApi() }
    private val historyManager by lazy { ExecutionHistoryManager.getInstance(AppContext.get()) }
    
    override suspend fun executeWork(): WorkResult {
        return try {
            // 遍历岛屿ID 0-4，依次请求每个岛屿的信息
            for (islandId in 0..4) {
                val islandQueryResult = gameApi.queryFactionArmy(islandId)
                val shouldContinue = islandQueryResult.foldWithApiException(
                    onSuccess = { islandResponse ->
                        // 处理当前岛屿的节点信息，返回是否应该继续
                        processIslandNodes(islandResponse, islandId)
                    },
                    onFailure = { code, msg ->
                        historyManager.addHistory(name, msg, code)
                        false
                    }
                )
                
                // 如果当前岛屿处理返回false，说明遇到进行中的节点，终止整个流程
                if (!shouldContinue) {
                    break
                }
            }
            
            WorkResult.Success
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            WorkResult.Failure(e, e.message)
        }
    }
    
    /**
     * 处理单个岛屿的节点信息
     * @return true表示应该继续处理下一个岛屿，false表示应该终止整个流程
     */
    private suspend fun processIslandNodes(response: QueryFactionArmyResponse, islandId: Int): Boolean {
        // 遍历当前岛屿的节点信息
        for (islandInfo in response.fightInfo.islandInfo) {
            // 如果节点状态为进行中，则终止整个流程
            if (islandInfo.pointStatus == 1) {
                return false // 返回false表示应该终止整个流程
            }
            
            // 检查节点状态为可领奖
            if (islandInfo.pointStatus == 2 && islandInfo.awardStatus == 1) {
                val pointAwardResult = gameApi.getFactionArmyPointAward(islandInfo.pointId)
                pointAwardResult.foldWithApiException(
                    onSuccess = { awardResponse ->
                        historyManager.addHistory(name, awardResponse.getDescription())
                    },
                    onFailure = { code, msg ->
                        historyManager.addHistory(name, msg, code)
                        false
                    }
                )
            }
        }
        
        // 检查当前岛屿的奖励状态
        if (response.fightInfo.islandAwardStatus == 1) {
            val islandAwardResult = gameApi.getFactionArmyIslandAward(islandId)
            islandAwardResult.foldWithApiException(
                onSuccess = { awardResponse ->
                    historyManager.addHistory(name, awardResponse.getDescription())
                },
                onFailure = { code, msg ->
                    historyManager.addHistory(name, msg, code)
                    false
                }
            )
        }
        
        return true // 返回true表示应该继续处理下一个岛屿
    }
}